package com.umeox.watch.audiobooks.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.umeox.watch.audiobooks.R;
import com.yc.music.model.AudioBean;

import java.util.ArrayList;
import java.util.List;

public class HotStoriesAdapter extends RecyclerView.Adapter<HotStoriesAdapter.Holder> {

    private final Context mContext;
    private final LayoutInflater mInflater;
    private int mLayoutId;

    static RequestOptions requestOptions = new RequestOptions()
            .centerCrop()
            .placeholder(R.drawable.cover_default)
            .diskCacheStrategy(DiskCacheStrategy.ALL);

    private List<AudioBean> mAudios = new ArrayList<>();
    private OnClickListener mOnClickListener;

    public HotStoriesAdapter(Context context, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = mInflater.inflate(mLayoutId, parent, false);
        return new Holder(itemView);
    }


    @Override
    public void onBindViewHolder(Holder holder, int position) {

        if (mAudios.isEmpty()) {
            return;
        }

        final AudioBean item = mAudios.get(position);
        holder.bind(mContext, item);
        holder.itemView.setOnClickListener(
                v -> {
                    if (mOnClickListener != null) {
                        mOnClickListener.onPositionClick(position);
                    }
                });

    }

    @Override
    public int getItemCount() {
        return mAudios.size();
    }




    public List<AudioBean> getAudioInfos() {
        return mAudios;
    }

    public void setAudioDatas(List<AudioBean> audioBooks) {
        mAudios.clear();
        mAudios.addAll(audioBooks);
        notifyDataSetChanged();
    }


    static class Holder extends RecyclerView.ViewHolder {
        ImageView mCoverIv;

        public Holder(final View itemView) {
            super(itemView);
            mCoverIv = itemView.findViewById(R.id.iv_cover);
        }

        public void bind(Context mContext, AudioBean item) {
            Glide.with(mContext).load(item.getCoverPath()).apply(requestOptions).into(mCoverIv);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onPositionClick(int position);
    }
}
